package com.zcm.springbootwebsocket.controller;

import com.zcm.springbootwebsocket.entity.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author Zhang Dapao
 * @date 2022/4/9
 */
@Controller

public class LoginController {
    @RequestMapping(value = {"/","/login"},method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }
    @RequestMapping(value = {"/main"},method = RequestMethod.GET)
    public String toMain(){
        return "main";
    }
    @RequestMapping("/toLogin")
    @ResponseBody
    public Result tologin(@RequestParam("user") String user, @RequestParam("pwd") String pwd, HttpSession session){
        Result result = new Result();
        if (user.equals("张三")&&pwd.equals("123")){
            result.setFlag(true);
            session.setAttribute("user",user);
        }else if (user.equals("李四")&&pwd.equals("123")){
            result.setFlag(true);
            session.setAttribute("user",user);
        }else if (user.equals("123")&&pwd.equals("123")){
            result.setFlag(true);
            session.setAttribute("user",user);
        }
        else if (user.equals("王五")&&pwd.equals("123")){
            result.setFlag(true);
            session.setAttribute("user",user);
        }else {
            result.setFlag(false);
            result.setMessage("登录失败");
        }
        return result;
    }

    @RequestMapping("/getUsername")
    @ResponseBody
    public String getUsername(HttpSession session){
        String username = (String) session.getAttribute("user");
        return username;
    }
}
